﻿using EkStudio.com.bean.xml.RootInterface;
using System;
using System.Collections.Generic;
using EkStudio.com.bean.xml.RootInterface.DataSource;
using EkStudio.com.bean.xml.BillModule;
using EkStudio.com.bean.xml.reflection;
using EkStudio.com.common.unit;
using EkStudio.com.bean.xml.RootInterface.FieldEvent;
using EkStudio.com.bean.xml.RootInterface.Plugin;
using EkStudio.com.bean.xml.RootInterface.language;
using EkStudio.com.bean.xml.RootInterface.Field;
using EkStudio.com.bean.xml.ProjectVersion;

namespace EkStudio.com.bean.xml.BillMultiple
{
    [XmlRootElement("root")]
    public class BillMultipleItem : BillFormatModule
    {
        private bool readOnly = false; //单据的只读方式

        private BillModuleRoot multProperties = null; //单据的基础信息

        private BillMultipleParamRoot paramProperties = null; //单据的全局参数

        private BillSourceRoot dataSource = null;//单据的数据源

        private BillUiGroupRoot uiField = null; //字段集

        private BillDataFillRoot dataFill = null;//事件

        private BillExpandCodeRoot expandCode = null;//扩展代码

        private BillFieldsI18nRoot i18nProperties = null;//多语言集合


        [XmlElement]
        public BillModuleRoot MultProperties
        {
            get
            {
                return multProperties;
            }

            set
            {
                multProperties = value;
            }
        }
        [XmlElement]
        public BillMultipleParamRoot ParamProperties
        {
            get { return paramProperties; }
            set { paramProperties = value; }
        }
        [XmlElement("dataProperties")]
        public BillSourceRoot DataSource
        {
            get
            {
                return dataSource;
            }

            set
            {
                dataSource = value;
            }
        }
        [XmlElement("fieldsProperties")]
        public BillUiGroupRoot UiField
        {
            get { return uiField; }
            set { uiField = value; }
        }

        [XmlElement]
        public BillDataFillRoot DataFill
        {
            get { return dataFill; }
            set { dataFill = value; }
        }
        [XmlElement]
        public BillExpandCodeRoot ExpandCode
        {
            get { return expandCode; }
            set { expandCode = value; }
        }
        [XmlElement]
        public BillFieldsI18nRoot I18nProperties
        {
            get { return i18nProperties; }
            set { i18nProperties = value; }
        }


        [XmlTransient]
        public object ParentBean
        {
            get { return null; }
            set { }
        }

        /// <summary>
        /// 初始化多语言
        /// </summary>
        /// <param name="version"></param>
        public void autoInitLanguage(ProjectVersionItem version)
        {
            if (i18nProperties == null)
            {
                i18nProperties = new BillFieldsI18nRoot();
            }
            if (i18nProperties.Group == null)
            {
                i18nProperties.Group = new List<BillFieldsI18nGroup>();
            }
            List<BillFieldsI18nGroup> groups = i18nProperties.Group;
            if (version != null && version.Languages != null && version.Languages.Item != null)
            {
                List<ProjectLanguageElement> languages = version.Languages.Item;
                for (int i = 0; i < languages.Count; i++)
                {
                    string key = languages[i].Name;
                    string default_ = languages[i].Default_;
                    BillFieldsI18nGroup group = null;
                    for (int k = 0; k < groups.Count; k++)
                    {
                        if (groups[k].Key == key)
                        {
                            group = groups[k];
                            break;
                        }
                    }
                    if (group == null)
                    {
                        group = new BillFieldsI18nGroup();
                        group.Key = key;
                        group.Element = new List<BillFieldsI18nElement>();
                        groups.Add(group);
                    }
                    if (group.Element == null) group.Element = new List<BillFieldsI18nElement>();
                    this.createLanElement(group, (default_ == "Y"));

                    this.createDataFillElement(group, (default_ == "Y"));

                    this.createPluginElement(group, default_ == "Y");
                }

            }

        }



        public object Clone()
        {
            BillMultipleItem clone = new BillMultipleItem();

            return clone;
        }

        public int getBillType()
        {
            return ProjectUtil.TYPE_BILL_MULTIPLE;
        }

        public string getCaption()
        {
            return multProperties.Caption;
        }

        public string getJavaPackage()
        {
            return multProperties.JavaPackage;
        }

        public string getKey()
        {
            return multProperties.Key;
        }

        public string getLanguageText()
        {
            return multProperties.LanguageTexts;
        }

        public string getVersion()
        {
            return multProperties.RootVersion;
        }

        public bool isReadOnly()
        {
            return readOnly;
        }

        public void setReadOnly(bool read)
        {
            readOnly = read;
        }


        /// <summary>
        /// 字段组多语言
        /// </summary>
        /// <param name="group"></param>
        /// <param name="default_"></param>
        private void createLanElement(BillFieldsI18nGroup group, bool default_)
        {
            //添加字段组及字段多语言
            if (this.uiField != null && this.uiField.FieldsProperties != null)
            {
                List<BillUiGroupItems> fieldGroup = this.uiField.FieldsProperties;
                for (int i = 0; i < fieldGroup.Count; i++)
                {
                    BillUiGroupItems items = fieldGroup[i];
                    //添加字段组多语言
                    if (!existLanguage(group, items.LanguageTexts, (default_ ? items.Caption : "")))
                    {
                        BillFieldsI18nElement groupEle = new BillFieldsI18nElement();
                        groupEle.Key = items.LanguageTexts;
                        groupEle.Value = default_ ? items.Caption : "";
                        group.Element.Add(groupEle);
                    }
                    //添加字段多语言
                    if (items.Fields != null && items.Fields.Count > 0)
                    {
                        List<BillUiFieldsItem> item = items.Fields;
                        for (int k = 0; k < item.Count; k++)
                        {
                            BillUiFieldsItem field = item[k];
                            if (!existLanguage(group, field.LanguageTexts, default_ ? field.Caption : ""))
                            {
                                BillFieldsI18nElement ele = new BillFieldsI18nElement();
                                ele.Key = field.LanguageTexts;
                                ele.Value = default_ ? field.Caption : "";
                                group.Element.Add(ele);
                            }
                        }
                    }

                }
            }

        }

        /// <summary>
        /// 数据填充
        /// </summary>
        /// <param name="group"></param>
        /// <param name="default_"></param>
        private void createDataFillElement(BillFieldsI18nGroup group, bool default_)
        {
            if (this.dataFill != null && this.dataFill.LinkEvents != null)
            {
                List<BillDataFillItem> fillList = this.dataFill.LinkEvents;
                for (int i = 0; i < fillList.Count; i++)
                {
                    BillDataFillItem item = fillList[i];
                    if (item.LanguageTexts != "" && !existLanguage(group, item.LanguageTexts, default_ ? item.Caption : ""))
                    {
                        BillFieldsI18nElement ele = new BillFieldsI18nElement();
                        ele.Key = item.LanguageTexts;
                        ele.Value = default_ ? item.Caption : "";
                        group.Element.Add(ele);
                    }

                }
            }
        }
        /// <summary>
        /// 扩展代码多语言
        /// </summary>
        /// <param name="group"></param>
        /// <param name="default_"></param>
        private void createPluginElement(BillFieldsI18nGroup group, bool default_)
        {
            if (this.expandCode != null)
            {
                BillExpandCodeRoot root = this.expandCode;
                if (root.JavaPlugin != null && root.JavaPlugin.JavaItem != null)
                {
                    List<BillExpandJavaCodeItem> javaList = root.JavaPlugin.JavaItem;
                    for (int i = 0; i < javaList.Count; i++)
                    {
                        BillExpandJavaCodeItem item = javaList[i];
                        if (item.LanguageTexts != "" && !existLanguage(group, item.LanguageTexts, default_ ? item.Caption : ""))
                        {
                            BillFieldsI18nElement ele = new BillFieldsI18nElement();
                            ele.Key = item.LanguageTexts;
                            ele.Value = default_ ? item.Caption : "";
                            group.Element.Add(ele);
                        }
                    }
                }
                if (root.Jscript != null && root.Jscript.Item != null)
                {
                    List<BillExpandJsCodeItem> jsList = root.Jscript.Item;
                    for (int i = 0; i < jsList.Count; i++)
                    {
                        BillExpandJsCodeItem item = jsList[i];
                        if (item.LanguageTexts != "" && !existLanguage(group, item.LanguageTexts, default_ ? item.Caption : ""))
                        {
                            BillFieldsI18nElement ele = new BillFieldsI18nElement();
                            ele.Key = item.LanguageTexts;
                            ele.Value = default_ ? item.Caption : "";
                            group.Element.Add(ele);
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 判断多语言是否已经存在
        /// </summary>
        /// <param name="group"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        private bool existLanguage(BillFieldsI18nGroup group, string key, string caption)
        {
            bool result = false;
            if (group != null && group.Element != null)
            {
                foreach (BillFieldsI18nElement i18 in group.Element)
                {
                    if (i18.Key == key)
                    {
                        if (caption != "")//存在时，如果是默认语言则修改为当前
                        {
                            i18.Value = caption;
                        }
                        result = true;
                        break;
                    }
                }
            }

            return result;
        }
    }
}
